import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'
import { getToken } from '@/utils/auth'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'


// 常量路由（无论是什么人，都能访问）
export const constantRoutes = [
  // 登录
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  // 首页
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },
]
// 异步路由
export const asyncRoutes = [
  // 权限管理模块
  {
    path: '/acl',
    component: Layout,
    name: 'Acl',
    redirect: '/acl/user',
    meta: {
      title: '权限管理',
      icon: 'el-icon-lock'
    },
    children: [{
      path: 'user',
      name: 'User',
      component: () => import('@/views/Acl/User/index'),
      meta: { title: '用户管理' }
    },
    {
      path: 'role',
      name: 'Role',
      component: () => import('@/views/Acl/Role/index'),
      meta: { title: '角色管理' }
    },
    {
      path: 'permission',
      name: 'Permission',
      component: () => import('@/views/Acl/Permission/index'),
      meta: { title: '菜单管理' }
    },
    {
      path: 'auth/:id',
      name: 'RoleAuth',
      component: () => import('@/views/Acl/RoleAuth/index'),
    },]
  },

  // 商品管理模块
  {
    path: '/product',
    component: Layout,
    name: 'Product',
    redirect: '/product/attr',
    meta: {
      title: '商品管理',
      icon: 'el-icon-goods'
    },
    children: [{
      path: 'attr',
      name: 'Attr',
      component: () => import('@/views/Product/Attr/index'),
      meta: { title: '平台属性管理' }
    },
    {
      path: 'spu',
      name: 'Spu',
      component: () => import('@/views/Product/Spu/index'),
      meta: { title: 'Spu管理' }
    },
    {
      path: 'sku',
      name: 'Sku',
      component: () => import('@/views/Product/Sku/index'),
      meta: { title: 'Sku管理' }
    }]
  },
]

// 任意路由：当路径出现错误的时候重定向404
export const anyRoutes = { path: '*', redirect: '/404', hidden: true }

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

// router.beforeEach(async (to, from, next) => {
//   // 获取token，没有获取退出和重新登录会报错
//   let token = getToken()
//   if (token) {
//     // 当用户登录，不能显示登录页面，直接去首页
//     if (to.path === '/login') {
//       next({path: '/'})
//     }
//     // 判断是否刷新了页面
//     if (!store.state.user.isAddRoute) {
//       // 是，则重新派发获取用户数据跟动态添加路由的actions
//       await store.dispatch('user/getInfo')
//       next({
//         ...to,
//         replace: true
//       })
//     } else {
//       next()
//     }
//   } else {
//     if (to.path !== '/login') {
//       next({path: '/login'})
//     } else {
//       next()
//     }
//   }
// });
export default router
